package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.IdentifyfileMapper;
import com.hbnu.system.model.entity.Identifyfile;
import com.hbnu.system.service.IIdentifyfileService;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Paperfile)表服务实现类
 *
 * @author makejava
 * @since 2023-08-15 13:46:42
 */
@Service("identifyfileService")
public class IdentifyfileServiceImpl extends ServiceImpl<IdentifyfileMapper, Identifyfile> implements IIdentifyfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;

    @Override
    public boolean addIdentifyfile(Identifyfile identifyfile) {
        identifyfile.setFiledate(LocalDateTime.now());
        return baseMapper.insert(identifyfile)==1;
    }

    @Override
    public Identifyfile getIdentifyfile(String id) {
        Identifyfile identifyfile = super.getOne(Wrappers.lambdaQuery(Identifyfile.class)
                .eq(Identifyfile::getFileid,id));
        if(Objects.isNull(identifyfile)){
            throw  new BaseException("文件编号异常！未查找到该文件信息");
        }
        return identifyfile;
    }

    @Override
    public List<Identifyfile> pageIdentifyfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("成果鉴定编号为空值");
        }
        List<Identifyfile> identifyfileList = baseMapper.selectList(Wrappers.lambdaQuery(Identifyfile.class)
                .eq(Identifyfile::getIdentifyid,id));
        return identifyfileList;
    }

    @Override
    public boolean deletIdentifyfile(String id) throws IOException {
       Identifyfile one = super.getOne(Wrappers.lambdaQuery(Identifyfile.class)
               .eq(Identifyfile::getFileid,id));
       if(Objects.isNull(one)){
           throw  new BaseException("文件编号有误！");
       }
       boolean delete = fileUploadUtil.delete(one.getFileurl());
       if(!delete){
           throw  new BindException("附件删除失败");
       }
       return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateIdentifyfile(Identifyfile identifyfile) {
        return baseMapper.updateById(identifyfile) == 1;
    }
}

